package com.sayice.robotconflict.game.object.logic;

import com.sayice.robotconflict.game.object.Robot;

/**
 * 机器人逻辑
 *
 * @author shubing
 */
public class Logic {
    public Condition condition;
    
    public Task task;
    
    public void execute(Robot robot) {
        if (condition == null || condition.ready(robot)) {
            task.run(robot);
        }
    }
}

class Condition {
    public ConditionBody body;
    
    public SubConds subConds;
    
    public boolean ready(Robot robot) {
        return body == null ? subConds.getValue(robot) : body.getValue(robot);
    }
}

class Task {
    public SetFunc func;
    
    public Object[] params;
    
    public void run(Robot robot) {
        func.set(robot, params);
    }
}

class ConditionBody {
    public Operand operand1;
    
    public Operand operand2;
    
    public CompareOperator operator;
    
    public <T extends Comparable<T>> boolean getValue(Robot robot) {
        T value1 = operand1.getValue(robot);
        T value2 = operand2.getValue(robot);
        return operator.apply(value1, value2);
    }
}

class SubConds {
    public Condition subCond1;
    
    public Condition subCond2;
    
    public LogicOperator operator;
    
    public boolean getValue(Robot robot) {
        return operator.apply(subCond1.ready(robot), subCond2.ready(robot));
    }
}

class Operand {
    public SimpleGetFunc func;
    
    public Object[] params;
    
    public <T extends Comparable<T>> T getValue(Robot robot) {
        return func.get(robot, params);
    }
}

